package cn.wolfcode.controller;

import cn.wolfcode.query.JsonResult;
import io.swagger.annotations.Api;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author jishushen
 * @create 2021-02-18 18:15
 * 登录的controller
 */
@Api(value = "登录接口",description = "登录源控制器")
@Controller
@RequestMapping("/login")
public class LoginController {

    @PostMapping("/login")
    @ResponseBody
    public JsonResult login(String username, String password){
        try {
            //获取令牌
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
            //获取用户主体
            SecurityUtils.getSubject().login(token);
            System.out.println(SecurityUtils.getSubject().isAuthenticated());
            return new JsonResult(true,"登录成功");
        } catch (UnknownAccountException e) {
            return new JsonResult(false,"用户名不正确");
        }catch (IncorrectCredentialsException e){
            return new JsonResult(false,"密码不正确");
        }catch (Exception e){
            return new JsonResult(false,"出错啦！");
        }

    }
}
